//
//  AssessmentController.m
//  shike
//
//  Created by RegentSoft on 2017/4/21.
//  Copyright © 2017年 RegentSoft. All rights reserved.
//

#import "AssessmentController.h"
#import "AssessTestTableViewCell.h"
#import "BeginTestController.h"
#import "QueryNumberController.h"
@interface AssessmentController ()<UITableViewDelegate,UITableViewDataSource>{

    NSInteger  integerIndex;//记录选择的UISegmentedControl
    UITableView * testTabView;
}

@end

@implementation AssessmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏的标题颜色字体
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#999999"];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor colorWithHexString:@"#323232"],NSFontAttributeName :[UIFont systemFontOfSize:16.0]}];
    self.title = @"全部考试";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F0F0F5"];
    NSArray *segmentArray = @[@"已考",@"未考"];
    // 初始化UISegmentedControl
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segmentArray];
    segmentControl.frame = CGRectMake((DeviceWidth-160)/2,80, 160, 30);
    // 设置默认选择项索引
    segmentControl.selectedSegmentIndex = 0;
    segmentControl.tintColor = [UIColor colorWithHexString:@"#61CAFD"];
    // 设置在点击后是否恢复原样
    segmentControl.momentary = NO;
    // 添加监听
    [segmentControl addTarget:self action:@selector(didClickSegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    
    testTabView = [[UITableView alloc] initWithFrame:CGRectMake(0,120, DeviceWidth, DeviceHeight-120) style:UITableViewStylePlain];
    testTabView.delegate = self;
    testTabView.dataSource = self;
    [self.view addSubview:testTabView];
    
}
//监听点击了哪项
- (void)didClickSegmentedControlAction:(UISegmentedControl *)segmentControl
{
    integerIndex = segmentControl.selectedSegmentIndex;
    [testTabView reloadData];
}
//区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (integerIndex == 0) {
        return 1;
    }else{
        return 2;
    }
}
//区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (integerIndex == 0) {
        return 0;
    }else{
        return 30;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 30)];
    view.backgroundColor = [UIColor colorWithHexString:mainBgColor];
    UILabel * titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 60, 20)];
    titleLabel.text = @"模块";
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    titleLabel.font = [UIFont systemFontOfSize:12.0];
    [view addSubview:titleLabel];
    return view;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdenifer =@"cellIdenifer";
    AssessTestTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellIdenifer];
    if (!cell) {
        cell =[[AssessTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdenifer];
    }
    if (integerIndex == 1) {
        [cell.contentBtn setTitle:@"考试" forState:UIControlStateNormal];
        cell.headImage.image = [UIImage imageNamed:@"appraisalTest"];
        cell.titleLabel.text = @"销售技巧考核";
        cell.contentBtn.tag =indexPath.row + 1000;
        [cell.contentBtn addTarget:self action:@selector(seeTestClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [cell.contentBtn setTitle:@"成绩查询" forState:UIControlStateNormal];
        cell.headImage.image = [UIImage imageNamed:@"appraisalTest"];
        cell.titleLabel.text = @"销售技巧考核";
        cell.contentBtn.tag =indexPath.row + 1001;
        [cell.contentBtn addTarget:self action:@selector(seeTestClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return cell;
}
-(void)seeTestClick:(UIButton *)sender{
    if ( integerIndex == 1) {
        BeginTestController * beginView =[[BeginTestController alloc] init];
        beginView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:beginView animated:YES];
    }else{
        QueryNumberController * queryView =[[QueryNumberController alloc] init];
        queryView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:queryView animated:YES];
    }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{

    if ( integerIndex == 1) {
        BeginTestController * beginView =[[BeginTestController alloc] init];
        beginView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:beginView animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
